<template>
  <div>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmpassword">
        <el-input type="password" v-model="ruleForm.confirmpassword"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
        <router-link :to="{name: 'Blogs'}">
          <el-button style="margin: 0px 10px">
            返回主页
          </el-button>
        </router-link>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入确认密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致！"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        id:'',
        password: '',
        confirmpassword: '',
      },
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            pattern: /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/,
            message: "6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种",
            trigger: "blur"
          }
        ],
        confirmpassword: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]

      }
    };

  },
  methods: {

    submitForm(formName) {
      const userId = this.$route.params.userId
      console.log(userId)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.ruleForm.id=userId
          console.log(this.ruleForm);
          this.$axios.put('/user/updatepassword', this.ruleForm,{
            headers: {
              "Authorization": localStorage.getItem("token")
            }
          }).then(res => {
            console.log(res)
            _this.$alert('修改密码成功', '提示', {
              confirmButtonText: '确定',
              //调回列表页
              callback: action => {
                _this.$router.push("/blogs")
              }
            });
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>